import { createRouter, createWebHistory } from 'vue-router';

// 基础路由
let routes = [
  {
    path: '/',
    name: 'initial',
    component: () => import('../views/initial.vue'),
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home.vue'),
  },
];

// 加载modules下所有路由文件
let modelsFile = import.meta.glob('./modules/*.js'),
  modelsArr = Object.values(modelsFile);
for (let modelsIndex in modelsArr) {
  let modelsItem = await modelsArr[modelsIndex]();
  routes.push.apply(routes, modelsItem.default);
}

// 导出路由
const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  vm.config.globalProperties.$store_router.setState(
    'routerIndex',
    window.$core.arrFindIndex(tabbarArr, 'name', to.name)
  );
  next();
});

export const tabbarArr = [
  {
    name: 'circle_index',
    icon: 'home-o',
    title: '圈子',
  },
  {
    name: 'sport_index',
    icon: 'smile-comment',
    title: '运动',
  },
  {
    name: 'match_index',
    icon: 'cluster',
    title: '赛事',
  },
  {
    name: 'my_index',
    icon: 'manager',
    title: '我的',
  },
];

export default router;
